package com.ywjh.inter.servlet;
import com.ywjh.inter.entity.CholesterolEntity;
import com.ywjh.inter.service.PatientDataService;
import com.ywjh.inter.utils.StrUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.List;

/**
 * * 胆固醇接口
 */
public class CholesterolServlet extends HttpServlet {

    private static final long serialVersionUID = -1737303311126608455L;

    private static final double LOW = 2.59;
    private static final double HIGH = 10.35;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=utf-8");
        int r = 0;
        String e = "";

        JSONObject postData = StrUtil.getPostJSON(req);
        String act = postData.getString("act");
        JSONObject data = postData.getJSONObject("data");
        JSONObject json = new JSONObject();
        System.out.println("cholesterol="+postData.toString());
        switch (act) {
            case "add":
                int userId = data.getInt("user_id");
                float cholesterolData = StrUtil.getFloat(data.getString("data"), 0.0f);
                Timestamp time = StrUtil.getTimestampHHMISS(data.getString("time"));
                //String memo = data.getString("memo");
                String source = data.getString("source");
                CholesterolEntity entity = new CholesterolEntity();
                entity.setCheck_time(time);
                entity.setAdd_time(new Timestamp(System.currentTimeMillis()));
                entity.setData(cholesterolData);
                entity.setStatus(1);
                entity.setUser_id(userId);
                entity.setMemo("default");
                entity.setData_source(source);
                boolean b = PatientDataService.getInstance().addCholesterol(entity);
                if (b) {
                    r = 1;
                } else {
                    e = "数据库错误";
                }
                break;
            case "data":
                userId = data.getInt("user_id");
                String startTime = data.getString("start_time");
                String endTime = data.getString("end_time");
                List<CholesterolEntity> list= PatientDataService.getInstance().ListCholesterolByTime(userId,startTime,endTime);
                JSONArray array=new JSONArray();
                for(CholesterolEntity cholesterol:list) {
                    JSONObject obj=new JSONObject();
                    obj.put("id", cholesterol.getId());
                    obj.put("data", cholesterol.getData());
                    obj.put("check_time", StrUtil.getYYYYMMDDHHMISS(cholesterol.getCheck_time()));
                    obj.put("add_time", StrUtil.getYYYYMMDDHHMISS(cholesterol.getAdd_time()));
                    obj.put("memo", cholesterol.getMemo());
                    obj.put("data_source", cholesterol.getData_source());
                    if (cholesterol.getData() < LOW) {
                        obj.put("is_warning", 1);// 低
                    } else if (cholesterol.getData() > HIGH) {
                        obj.put("is_warning", 2);// 高
                    } else {
                        obj.put("is_warning", 0);// 正常
                    }
                    array.add(obj);
                }
                r=1;
                json.put("d", array);
                break;

            case "count":
                userId = data.getInt("user_id");
                startTime = data.getString("start_time");
                endTime = data.getString("end_time");
                list= PatientDataService.getInstance().ListCholesterolByTime(userId,startTime,endTime);
                JSONObject obj=new JSONObject();
                DecimalFormat df = new DecimalFormat("#.##");
                double low = 0;
                double high = 0;
                double normal = 0;
                String percentageLow = "0";
                String percentageHigh = "0";
                String percentageNormal= "0";
                String str1;
                String str2;
                for(CholesterolEntity cholesterol:list) {
                    float dataCholesterol = cholesterol.getData();
                    if(dataCholesterol<LOW){
                        low++;
                    }else if(dataCholesterol>HIGH){
                        high++;
                    }else{
                        normal++;
                    }
                }
                double all = low+high+normal;
                if( all!=0 ){
                    str1 = df.format(low/all);
                    double d1 = Double.valueOf(str1).doubleValue()*100;
                    percentageLow = d1+"";
                    percentageLow = percentageLow.substring(0,2);

                    str2 = df.format(high/all);
                    double d2 = Double.valueOf(str2).doubleValue()*100;
                    percentageHigh = d2+"";
                    percentageHigh = percentageHigh.substring(0,2);

                    percentageNormal = String.valueOf(100-d1-d2);
                    percentageNormal = percentageNormal.substring(0,2);

                }

                obj.put("low",low);
                obj.put("high",high);
                obj.put("normal",normal);
                obj.put("percentageLow",percentageLow+"%");
                obj.put("percentageHigh",percentageHigh+"%");
                obj.put("percentageNormal",percentageNormal+"%");

                r=1;
                json.put("d", obj);
                break;

        }

        json.put("r", r);
        json.put("e", e);
        PrintWriter pw = resp.getWriter();
        pw.write(json.toString());
        pw.flush();
        pw.close();
    }
}
